

import com.lcn.comm.framework.task.ConditionUtils;
import com.lcn.comm.framework.task.ICallBack;
import com.lcn.comm.util.DateUtil;
import com.lcn.tm.TxManagerApplication;
import com.weibo.api.motan.util.LoggerUtil;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.env.Environment;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

import javax.annotation.Resource;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * <p>类说明</p>
 *
 * @author 张峰 zfvip_it@163.com
 * @createTime: 2017/9/20 15:11
 */
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@SpringBootTest(classes = TxManagerApplication.class)// 指定spring-boot的启动类
public class SpringBootTest1 {

    @Resource
    private Environment environment;


    @Test
    public void test1() throws Throwable {
//        final MainTest.Task task = new MainTest.Task();
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                try {
//                    ;
//                    LoggerUtil.info("当前线程是否中断[{}]",Thread.currentThread().interrupted());
//                    Thread.sleep(2000);
//                    LoggerUtil.info("当前线程是否中断[{}]",Thread.currentThread().interrupted());
//
//                    task.signalTask();
//                    LoggerUtil.info("唤醒");
//                    Thread.sleep(5000);
//                    LoggerUtil.info("线程唤醒结束");
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//            }
//        }).start();
//        LoggerUtil.info("开始等待");
//        //等待线程
//        task.waitTask();
//        LoggerUtil.info("1当前线程是否中断[{}]",Thread.currentThread().interrupted());
//        LoggerUtil.info("运行结束");
//        Thread.sleep(6000);
        Long startTime = System.currentTimeMillis();
        //创建task
        LoggerUtil.info("开始创建Task");
        final String key = "aaaa";
        final com.lcn.comm.framework.task.Task task = ConditionUtils.getInstance().createTask(key);
        task.setCallBack(new ICallBack() {
            @Override
            public Object call(Object... obj) throws Throwable {
                return null;
            }
        });

        new Thread(new Runnable() {
            @Override
            public void run() {
                com.lcn.comm.framework.task.Task task1 = ConditionUtils.getInstance().getTask(key);
                while (!task1.isAwait() && !Thread.currentThread().interrupted()) {
                    try {
                        Thread.sleep(1);
                        LoggerUtil.info("继续休眠");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                //获取task

                LoggerUtil.info("状态" + task1.isAwait());
                if (task1.isAwait()) {
                    LoggerUtil.info("开始唤醒");
                    task1.setCallBack(new ICallBack() {
                        @Override
                        public Object call(Object... obj) throws Throwable {
                            return "唤醒成功";
                        }
                    });
                    task1.signalTask();
                }
            }
        }).start();

        LoggerUtil.info("当前线程名称" + Thread.currentThread().getName());
        LoggerUtil.info("线程开始等待");
        task.waitTask();
        LoggerUtil.info("测试返回值[{}]",task.getCallBack().call());
        LoggerUtil.info("执行耗时[{}]", DateUtil.dateDiff(startTime,System.currentTimeMillis()));
    }


    static class Task {
        private Lock lock = new ReentrantLock();
        Condition condition = null;

        public Task() {
            condition = lock.newCondition();
        }

        public void waitTask() throws InterruptedException {
            try {
                this.lock.lock();
                condition.await();
            } finally {
                this.lock.unlock();
            }
        }

        public void signalTask() {
            try {
                this.lock.lock();
                condition.signal();
            } finally {
                this.lock.unlock();
            }
        }
    }

}
